package com.project.service;

import com.project.dao.ClassMapper;
import com.project.entity.Class;
import com.project.entity.StudentInfo;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 12:36
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ClassServiceImpl extends BaseService implements ClassService {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private  ClassMapper classMapper;


    @Override
    public int add(Class classroom) {
        return classMapper.insertSelective(classroom);
    }

    @Override
    public int removeOne(Integer id) {
        return classMapper.deleteByPrimaryKey(id);
    }

    /**
     * Todo 如何判断是否成功
     * author: WheelChen
     * date: 2017/9/16
     * function: 删除班级及班级下的所有学生信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeOneChain(Integer id) {
        //先删除班级表记录
        removeOne(id);
        //查出班级下的学生表信息
        List<StudentInfo> studentInfoList = studentInfoService.findBatchByClassId(id);
        //删除学生表信息(先不采用单独删除的方式)
        //studentInfoService.removeBatchByClassId(id);
        if (studentInfoList.size() != 0)
            studentService.removeBatchChain(studentInfoList);

        return true;
    }

    @Override
    public int modify(Class classroom) {
        return classMapper.updateByPrimaryKeySelective(classroom);
    }

    @Override
    public Class findOne(Integer id) {
        return classMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/7/13
     * function: 根据专业id批量查找班级
     *
     * @param majorid 专业表id
     * @return
     */
    @Override
    public List<Class> findBatchByMajorid(Integer majorid) {
        return classMapper.selectByMajorid(majorid);
    }

    /**
     * author: WheelChen
     * date: 2017/7/25
     * function: 根据学院id批量查找班级
     *
     * @param schoolId 学院id
     * @return
     */
    @Override
    public List<Class> findBatchBySchoolId(Integer schoolId) {
        return classMapper.selectBatchBySchoolId(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查班级是否已存在
     *
     * @param clazz
     * @return true - 存在 false - 不存在
     */
    @Override
    public boolean checkClassIsExist(Class clazz) {
        return classMapper.checkClassIsExist(clazz) != 0;
    }

    /**
     * author: WheelChen
     * date: 2017/11/2
     * function: 根据专业id删除班级表
     *
     * @param majorId 专业id
     * @return
     */
    @Override
    public int removeByMajorId(Integer majorId) {
        return classMapper.deleteByMajorId(majorId);
    }

    /**
     * author: WheelChen
     * date: 2017/11/2
     * function: 根据系id删除班级表
     *
     * @param departmentId 系id
     * @return
     */
    @Override
    public int removeByDepartmentId(Integer departmentId) {
        return classMapper.deleteByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/11/3
     * function: 根据学校id批量删除
     *
     * @param schoolId 学校id
     * @return
     */
    @Override
    public int removeBatchBySchoolId(Integer schoolId) {
        return classMapper.deleteBatchBySchoolId(schoolId);
    }


}
